import store from '@/store'
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  // { path: '/', redirect: '/login' },
  { path: '/login', component: () => import('@/views/Login.vue') },
  { path: '/reg', component: () => import('@/views/Reg.vue') },
  {
    path: '/',
    component: () => import('@/views/Home.vue'),
    children: [
      {
        path: 'main',
        component: () => import('@/views/Main.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if ((to.path === '/login') | (to.path === '/reg')) {
    next()
  } else {
    if (store.state.user.token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
